/**
 * Created by Cake on 2017/5/24.
 */

var scrollFlag = true;

(function ($) {
    /* 登录表单操作 */
    $.fn.formOption = function (options) {

        var defaults = {
            oldColor: '#ddd',
            newColor: '#2e82ff'
        };

        var options = $.extend(defaults, options);

        this.each(function () {

            $(this)
                .bind('focus', function () {  //获取焦点变色
                    $(this).parent().css({
                        'border-color': options.newColor
                    });
                })
                .bind('input', function () {  //输入时显示清除按钮
                    var val = $(this).val();
                    if (val != '') {
                        $(this).next().show();
                    } else {
                        $(this).next().hide();
                    }
                })
                .bind('blur', function () {  //失去焦点变回原色
                    var val = $(this).val();
                    if (val == '') {
                        $(this).parent().css({
                            'border-color': options.oldColor
                        });
                    } else {
                        $(this).parent().css({
                            'border-color': options.newColor
                        });
                    }
                });

            $(this).eq(0).next().on('click', function () {  //清空输入框
                $(this)
                    .hide()
                    .prev().val('').focus();
            })
        });

    };

    /* 验证码倒数 */
    $.fn.timeCount = function (options) {

        var defaults = {
            count: 60
        };

        var options = $.extend(defaults, options);

        this.each(function () {

            $(this).bind('click', function () {
                var $this = $(this),
                    _setTime = function (object) {
                    if (options.count == 0) {
                        object
                            .attr('disabled',false)
                            .removeClass('code-grey')
                            .val( '获取');
                        options.count = 60;
                    } else {
                        object
                            .attr('disabled', true)
                            .addClass('code-grey')
                            .val(options.count + '秒');
                        options.count--;
                        setTimeout(function(){
                            _setTime(object);
                        },1000);
                    }
                };

                _setTime($this);
            });
        });
    };

    /* 滚动加载 */
    $.fn.loadScroller = function (options) {

        var defaults = {
            square: null, //滚动区域外的区域高度总和
            scrollOver: function () {}  //滚到到底部回调函数
        };

        var options = $.extend(defaults, options);

        this.each(function () {

            var $this = $(this);

            var _scrollLoad = function (object) {
                var p = 0, t = 0;
                object.bind('scroll', function () {
                    p = $(this).scrollTop();
                    if (t < p) {
                        var outer = parseInt($(this).height());
                        var inner = parseInt($(this).find('.scroll-con').height() - options.square);
                        var scroll = $(this).scrollTop();
                        if (outer + scroll >= inner && scrollFlag == true) {
                            scrollFlag = false;
                            options.scrollOver();
                        }
                    }

                    //将代码立即插入任务队列
                    setTimeout(function(){t = p;}, 0);
                });

            };

            _scrollLoad($this);
        });
    };

    /*大图浏览*/
    $.fn.showPicture = function () {

        var dom = $('.swiper-big-pic');
        if (dom.length > 0) {
            var swiper = new Swiper('.swiper-big-pic', {
                pagination: '.swiper-pagination',
                paginationType: 'fraction',
                observer:true,
                observeParents:true
            });
        }

        $(this).bind('click', function () {
            var arr = [],
                html = [],
                size = $(this).siblings().length + 1;

            if ($(this).length > 0) {
                $(this).parent().find('li').each(function () {
                    var src = $(this).find('img').attr('src');
                    arr.push(src);
                });
            }

            for (var i = 0; i < size; i++) {
                html.push('<div class="swiper-slide"><a href="javascript:;"><img src="'+ arr[i] +'"></a></div>');
            }

            $('.swiper-big-pic .swiper-wrapper').html(html.join(''));

            swiper.slideTo($(this).index(), 0, false);

            $('.big-pic-show').addClass('big-pic-show-open');
        });

        $('.big-pic-show').bind('click', function () {
            $(this).removeClass('big-pic-show-open');
        });

    };

    /* 编辑工人 */
    $.fn.editWorker = function () {
        $(this).bind('click', function () {
            if ($(this).text() == '完成') {
                $('.tool-check').hide();
                $(this).text('编辑');
                $('.tool-check').find('input').prop('checked', false);
                $('.check-all').text('全选');
                $('.add-opt').show();
                $('.delete-opt').hide();
            } else {
                $('.tool-check').show();
                $(this).text('完成');
                $('.add-opt').hide();
                $('.delete-opt').show();
            }
        });
    }

    /* 全选工人 */
    $.fn.checkAll = function () {

        $(this).bind('click', function () {
            var dom = $('.tool-check'),
                domCheck = $('.tool-check input:checked'),
                size = dom.length,
                checkSize = domCheck.length;

            if (checkSize >= size) {
                $(this).text('全选');
                dom.find('input').prop('checked', false);
            } else {
                $(this).text('反选');
                dom.find('input').prop('checked', true);
            }
        });

        $('.tool-check input').bind('click', function () {
            var dom = $('.tool-check'),
                domCheck = $('.tool-check input:checked'),
                size = dom.length,
                checkSize = domCheck.length;

            if (checkSize >= size) {
                $('.check-all').text('反选');
            } else {
                $('.check-all').text('全选');
            }
        });

        $('.btn-delete').bind('click', function () {
            
        });
    };

})(jQuery);

/* alert消息框 */
function alertMsg(msg) {
    var arr = new Array();
    arr.push('<div class="alert" font="12" flex="cross:center main:center">');
    arr.push('<div class="alert-con">'+ msg + '</div></div>');

    $('.alert').remove();

    $('body').append(arr.join(''));

    setTimeout(function () {
        $('.alert').addClass('alert-show');
    }, 20);

    setTimeout(function() {
        $('.alert').removeClass('alert-show');
    }, 2000);

}

/* confirm消息框 */
function confirmMsg(msg, callback) {
    var arr = new Array();
    arr.push('<div class="dialog-msg dialog-confirm">');
    arr.push('<div class="confirm">');
    arr.push('<div class="confirm-box" font="16">');
    arr.push('<div class="confirm-text">'+ msg +'</div>');
    arr.push('<div class="confirm-opt" flex="main:right">');
    arr.push('<a href="javascript:;" class="quite">取消</a>');
    arr.push('<a href="javascript:;" class="sure">确定</a>');
    arr.push('</div></div></div></div>');
    $('body').append(arr.join(''));

    setTimeout(function () {
        $('.dialog-confirm').addClass('dialog-show');
        $('.confirm-box').addClass('confirm-box-normal');
    }, 20);

    $('.quite').click(function () {
        $('.dialog-confirm').removeClass('dialog-show');
        $('.confirm-box').removeClass('confirm-box-normal');
        if (typeof (callback) == 'function') {
            callback(false);
        }
        setTimeout(function () {
            $('.dialog-confirm').remove();
        }, 400)
    });

    $('.sure').click(function () {
        $('.dialog-confirm').removeClass('dialog-show');
        $('.confirm-box').removeClass('confirm-box-normal');
        if (typeof (callback) == 'function') {
            callback(true);
        }
        setTimeout(function () {
            $('.dialog-confirm').remove();
        }, 400)
    })
}

/* 图片上传 */
function imgUpload (obj, callback) {
    $(obj).change(function () {
        lrz(this.files[0], {width: 400}, function (results) {
            var size = results.base64.length*0.8,
                src = results.base64;
            size = (size / 1024).toFixed(2);
            if (size <= 2048) {
                callback(src);
            } else {
                alertMsg('图片不能大于2M');
            }
        });
    });
}

$(function () {
    var domAdd = $('.add-btn');
    var domClose = $('.btn-close');
    var domChoice = $('.btn-choice');
    var domAddWorker = $('.btn-add-worker');
    var domChangeWorker = $('.btn-change-worker');
    var domGetMoney = $('.btn-get-money');
    var domGetProduct = $('.btn-get-product');
    var domAddPicture = $('.btn-add-pic');

    if (domAddPicture.length > 0) {
        domAddPicture.click(function () {
            $('.add-project-picture').fadeIn();
        });
    }

    if (domGetProduct.length > 0) {
        domGetProduct.click(function () {
            $('.get-product').fadeIn();
        });
    }

    if (domGetMoney.length > 0) {
        domGetMoney.click(function () {
            $('.get-money').fadeIn();
        });
    }

    if (domAdd.length > 0) {
        domAdd.click(function () {
            $('.add-project').fadeIn();
        });
    }

    if (domAddWorker.length > 0) {
        domAddWorker.click(function () {
            $('.add-worker').fadeIn();
        });
    }

    if (domChangeWorker.length > 0) {
        domChangeWorker.click(function () {
            $('.change-worker').fadeIn();
        });
    }

    if (domChoice.length > 0) {
        domChoice.click(function () {
           $('.list-select').fadeIn();
        });
    }

    if (domClose.length > 0) {
        domClose.click(function () {
            $(this).parent().parent().parent().fadeOut();
        });
    }
});
